#include "SolarSystem.h"
#include <sstream>

using namespace CAIG;
using namespace DKGL;



// Implementation of Scene
class SceneImpl
{
public:

	void init()
	{
		ground = new Grid(50, 50, 10, 10);
	}

private:
	Grid* ground;
	Scene* scene;
};

SolarSystem::SolarSystem() : mEarth("Earth"), mMoon("Moon"), mEarthCube("EarthCube"),
	m_pCamera(NULL), m_pMouse(NULL)
{
	Log::dump("SolarSystem Ctor");
}

bool SolarSystem::init()
{
	mBeFixCam = false;
	return (CWin::init());
}

void SolarSystem::createScene()
{
	
}

void SolarSystem::OnCreate()
{
	CreateDKGLContext(hDC);

    // Create Scene and Skybox
	Scene *scenePtr = g_SceneMgr.createScene("Scene");
    scenePtr->getSkyBox();

	m_pCamera = scenePtr->createCamera("Camera");
	m_pCamera->bindViewport(g_ViewportMgr.getViewportPtr());

	mEarth.setName("Earth");
	mMoon.setName("Moon");
	mEarthCube.setName("EarthCube");
	
	//m_pCamera->moveForward(-50);

	scenePtr->getRoot().pushChild(m_pCamera);
	Light& sun = g_LightMgr.getLight(0).enable(1);
	sun.setName("Sun");

	// Setup ground
	Grid* ground = new Grid(50, 50, 10, 10);
	ground->setRenderMode(RM_WIRE);
	ground->rotate(-90, Vector3::UNIT_X, LOCAL);
	scenePtr->getRoot().pushChild(ground);

	// Setup Tank
	tank = new Tank();
	scenePtr->getRoot().pushChild(tank);

	tank->pushChild(m_pCamera);
	m_pCamera->pitch(-90);
	m_pCamera->moveForward(-40);
	m_pCamera->rotate(45, Vector3::UNIT_X, DKGL::GLOBAL);

	
	scenePtr->getRoot().pushChild(&sun);
	sun.pushChild(&mEarth);
	mEarthCube.setSize(3);

	setMaterial();
	
	mEarth.translate(20, 0, 0, GLOBAL);
	mEarth.enableLighting(1);
	mEarth.pushChild(&mMoon);
	mEarth.pushChild(&mEarthCube);
	mMoon.translate(5.5, 0, 0, GLOBAL);
	mMoon.enableLighting();

	mEarth.setRadius(2);
	mMoon.setRadius(1);

	m_pMouse = new Mouse(m_pCamera);
	CWin::plugMouse(m_pMouse);

	//g_SceneMgr.destroyScene("MAIN_SCENE");
}

void SolarSystem::setMaterial()
{
	Material mat;

	mat.setDiffuse(Colorf::BLUE);
	mEarth.setMaterial(mat);
	
	mat.setDiffuse(Colorf::RED);			mEarthCube.setMaterial(mat, 0);
	mat.setDiffuse(Colorf::GREEN);		mEarthCube.setMaterial(mat, 1);
	mat.setDiffuse(Colorf::BLUE);		mEarthCube.setMaterial(mat, 2);
	mat.setDiffuse(Colorf::PURPLE);	mEarthCube.setMaterial(mat, 3);
	mat.setDiffuse(Colorf::YELLOW);	mEarthCube.setMaterial(mat, 4);
	mat.setDiffuse(Colorf::BLACK);		mEarthCube.setMaterial(mat, 5);

	mat.setAmbient(0.1f, 0.1f, 0.1f).setDiffuse(Colorf::YELLOW);
	mMoon.setMaterial(mat);
}

void SolarSystem::OnPaint()
{
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	g_SceneMgr.render();
	g_FontMgr.begin(g_ViewportMgr.getViewport(0));
	{
		std::stringstream ss;
		g_FontMgr.printf("w: move forward", 10, 20);//,
		g_FontMgr.printf("s: move backward", 10, 40);//,
		g_FontMgr.printf("a: shift left", 10, 60);//,
		g_FontMgr.printf("d: shift right", 10, 80);//,
		g_FontMgr.printf("q: toggle camera to earth", 10, 100);//,
		g_FontMgr.printf("esc: exit this demo", 10, 120);//,

		Vector3 p(tank->getWorldPosition());

		ss << "Tank:(" << p.x << ", " << p.y << ", " << p.z << ")";
		g_FontMgr.printf(ss.str(), 10, 140);//,
	}
	g_FontMgr.end();

	
}

void SolarSystem::OnSize(WORD width, WORD height)
{
	g_ViewportMgr.getViewportPtr()->onSize(
		intRect2D(0, 0, static_cast<int>(width), static_cast<int>(height)),
		DKGL::DefaultVpSizer());
}

void SolarSystem::KeyDown()
{
	if(!mBeFixCam)
	{
		if(mKeys[DK_W])
			m_pCamera->moveForward(5);
		else if(mKeys[DK_S])
			m_pCamera->moveForward(-5);
		else if(mKeys[DK_A])
			m_pCamera->moveRight(-5);
		else if(mKeys[DK_D])
			m_pCamera->moveRight(5);
		else if(mKeys[DK_1])
		{
			double fov = m_pCamera->getFov();
			m_pCamera->setFov(fov+1);
		}
		else if(mKeys[DK_2])
		{
			double fov = m_pCamera->getFov();
			m_pCamera->setFov(fov-1);
		}
		else if(mKeys[DK_O])
			m_pCamera->setProjectionMode(ORTHO);
		else if(mKeys[DK_P])
			m_pCamera->setProjectionMode(PERSPECTIVE);
		else if(mKeys[DK_UP])
			tank->moveForward(0.5f);
		else if(mKeys[DK_DOWN])
			tank->moveForward(-0.5f);
		else if(mKeys[DK_LEFT])
			tank->yaw(1.0f);
		else if(mKeys[DK_RIGHT])
			tank->yaw(-1.0f);

	}
/*
	if(mKeys[DK_Q])
	{
		mBeFixCam = !mBeFixCam;
		if(mBeFixCam)
		{
			m_pCamera->freeSelf();
			mEarth.pushChild(m_pCamera);
			m_pCamera->setPosition(0.0f, 0.0f, 0.0f);
		}
		else
		{
			mEarth.popChild("MAIN_CAMERA");
			g_SceneMgr.getScenePtr("MAIN_SCENE")->getRootPtr()->pushChild(m_pCamera);
		}

	}
*/		
	else if(mKeys[DK_ESCAPE])
		DestroyWindow(hWnd);
}

void SolarSystem::OnTime()
{
	mEarth.rotate(2, Vector3::UNIT_Y, GLOBAL);
	mEarth.rotate(-5, Vector3::UNIT_Y, LOCAL);
	mMoon.rotate(10, Vector3::UNIT_Y, GLOBAL);
}